<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/common/taglibs.jsp"%>
<%@taglib prefix="ah" uri="/WEB-INF/tags/utilTagLib.tld"%>
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="${res}/js/jquery/jquery.js"></script>
<script type="text/javascript" src="${res}/js/jquery/jquery.form.js"></script>
<script type="text/javascript" src="${res}/js/proposal/irisPrpPrepare.js"></script>
<script type="text/javascript" src="${res_app}/js/app/proposal/irisPrpPrepare.js"></script>
<script type="text/javascript" src="${res}/js/validate/irisvalidator.js"></script>
<title>
	<c:if test="${method_Get=='add'}">新增</c:if>
	<c:if test="${method_Get=='edit'}">编辑</c:if>
	限项
</title>
<%@ include file="/common/meta.jsp"%>
</head>
<body>
	<form id="mainForm" method="post" action="${ctx}/prpapprove/saveOrgLimit">
		<table width="100%" border="0" cellpadding="0" cellspacing="1" class="t_css" id="basic">
			<tr class="li_1">
				<td align="right" width="140">
				<span class="color_red">*</span>
					项目类别：
				</td>
				<td align="left"  width="200">
				    <%-- <c:set var="grantType">
		                {'id':'${grantCode}',
		                'name':'${grantName}'
		                }                   
		            </c:set>
                    <cpt:tree id="grantType" key="grant_codes" cacheable="false" className="hn_iuput_kuang required selectWidth3"
                        readonly="true" value="${grantType}" label="项目类别" showPattern="all" shieldPattern="0" onClick="selNode" checkable="true">
                    </cpt:tree> --%>
                    ${grantName}
                    <input type="hidden" id="grantType_value" class="input_css" name="id.grantCode" value="${id.grantCode}">
                    <input type="hidden" id="grantType_name" name="grantName" value="${grantName}">
				</td>
				<td align="right">
					业务年度：
				</td>
				<td align="left">
				<span>${id.statYear}</span>
					<input type="hidden" name="id.statYear" value="${id.statYear}" id="statYear" />
				</td>
			</tr>
			<tr class="li_2">
				<td align="right" >
					申报单位：
				</td>
				<td align="left">
				    <span>${orgName}</span>
				    <input type="hidden" name="orgName"    id="orgName" value="${orgName}"/>
                    <input type="hidden" name="id.orgCode" id="orgCode" value="${id.orgCode}" /> 
					<%--  
					<input type="hidden" id="recomend_org_value" name="id.orgCode" value="${id.orgCode}">
					<input type="hidden" id="recomend_org_name" name="orgName" value="${orgName}"> --%>
				</td>
				<td align="right" >
					申报批次：
				</td>
				<td align="left">
				 
					 <span><ah:const code="${id.batch}" category="batch"/></span>
                    <input type="hidden"  name="id.batch" value="${id.batch}" id="batch">
				</td>
			</tr>
			<tr class="li_1">
				<td align="right" >
					<span class="color_red">*</span>
					设置上限：
				</td>
				<td align="left">
					<input type="text" class="input_css required valueGENum:limitNum:1" name="limitNum" id="limitNum" label="设置上限"
						value="${limitNum}" onfocus="InitInput.setNumber(this,11,0,0);" style="text-align:right;"/>
				</td>
				<td align="right"></td>
				<td align="left"></td>
			</tr>
		</table>
		</table>
	</form>
	<table width="100%" align="center">
		<tr>
			<td>
				<input class="button_01" type="button" value="提交" onclick="save();" id="save">
				<input class="button_01" type="button" value="返回" onclick="parent.tb_remove2();">
			</td>
		</tr>
	</table>
</body>
</html>
<script type="text/javascript">
	var prcWin = new progressWin("${res}"); //初始化遮罩层
	function save() {
		validate($("#basic"), "限项");
		if (!valResult()) {
			irisValShowMsg($("#save"));
			return false;
		}
		
		// 判断设置的限制数是否小于已推荐数量
		var orgRecommendNum = getOrgRecommendNum();
		var limitNum = $('#limitNum').val();
		if(valResult() && (limitNum < orgRecommendNum)) {
            irisValaddMsg("单位已经推荐了" + orgRecommendNum + "个项目，设置项限不能小于" + orgRecommendNum);
            irisValShowMsg($("#save"));
            return false;
        }
		
		prcWin.openWin();//打开遮罩层
		$(":button,:submit").each(function() {//锁住按钮
			$(this).attr("disabled", "true");
		});
		var options = {
			success : function(msg) {
				prcWin.closeWin();
				if (msg != null && msg == "success") {//保存成功
					scmSuccess("保存成功", "100px");
					//parent.irisGrid2.getGridDom().jqGrid("refresh",0);
					parent.irisGrid.getGridDom().jqGrid("refresh", 0);
					window.setTimeout("parent.tb_remove();", 500);
				} else {
					scmError("保存失败", "100px");
					$(":button,:submit").each(function() { //打开按钮
						$(this).removeAttr("disabled");
					});
				}

			},// 表单成功提交后的回调函数
			error : function() {
				prcWin.closeWin();
				scmError("保存失败", "100px");
				$(":button,:submit").each(function() {//打开按钮
					$(this).removeAttr("disabled");
				});
			},
			dateType : 'text',
			type : "post" // 提交方式 post,get
		};

		$("#mainForm").ajaxSubmit(options);
	}

	function setValueById(name, obj, value) {
		var obj = $(obj);
		var nameId = name + "_name";
		var valueId = name + "_value";
		$("#" + nameId).val(obj.attr("name"));
		$("#" + valueId).val(obj.attr("id"));
	}

	function initTreeValueExt(treeId, treeNode) {
		
		if ((treeNode && treeNode.isParent)) {
			scmWarn("请选择具体的" + $("#" + treeId).attr("label"));
			
			$("#" + treeId).val("");
			$("#" + treeId).parents("td")
					.find(
							"[name$='" + treeId + "_code'],[id$='" + treeId
									+ "_code']").val("");
			$("#" + treeId).parents("td").find(
					"[name$='" + treeId + "_value'],[id$='" + treeId
							+ "_value']").val("");
			$("#" + treeId).parents("td")
					.find(
							"[name$='" + treeId + "_name'],[id$='" + treeId
									+ "_name']").val("");
			return false;
		}

		var str = treeNode.name;
		var obj = treeNode.parentNode;
		while (obj != null) {
			str = obj.name + "-" + str;
			obj = obj.parentNode;
		}

		//     initTitle(treeId);
		$("#" + treeId).parents("td").find(
				"[name$='" + treeId + "_code'],[id$='" + treeId + "_code']")
				.val(treeNode.id);
		$("#" + treeId).parents("td").find(
				"[name$='" + treeId + "_value'],[id$='" + treeId + "_value']")
				.val(treeNode.id);
		$("#" + treeId).parents("td").find(
				"[name$='" + treeId + "_name'],[id$='" + treeId + "_name']")
				.val(str);
	}
	
	function initTreeValueExt2(treeId, treeNode) {
        var str = treeNode.name;
        var obj = treeNode.parentNode;
        while (obj != null) {
            str = obj.name + "-" + str;
            obj = obj.parentNode;
        }
        
        $("#" + treeId).parents("td").find("[name$='" + treeId + "_code'],[id$='" + treeId + "_code']").val(treeNode.id);
        $("#" + treeId).parents("td").find("[name$='" + treeId + "_value'],[id$='" + treeId + "_value']").val(treeNode.id);
        $("#" + treeId).parents("td").find("[name$='" + treeId + "_name'],[id$='" + treeId + "_name']").val(str);
        $("#grantType").val(str);
        $("#grantType_hideName").val(str);
    }
	
	function selNode(id,node){
	    
// 	    selChildNode(node,node.checked==true);
	    var inputNameVal = "";
	    var hideIdVal = "";
	    var checkedNodes = zTreeObj.getCheckedNodes();
	 // 将所有的值按照从小到大排序
        checkedNodes.sort(function(num1, num2) {
                return num1.id - num2.id;
            });
	    for ( var i = 0; i < checkedNodes.length; i++) {
	        if(!checkedNodes[i].isParent){
	            if (inputNameVal == '') {
	                inputNameVal = checkedNodes[i].name;
	                hideIdVal = checkedNodes[i].id;
	            } else {
	                inputNameVal += "," + checkedNodes[i].name;
	                hideIdVal += "," + checkedNodes[i].id;
	            }
	        }
	    }
	    
	    $("#grantType_value").val(hideIdVal);
	    $("#grantType_name").val(inputNameVal);
	    
	    /* if ($("#" + inputId + "_text").length == 0) {
	        $("#" + inputId).val(inputNameVal);
	        $("#" + hiddenId).val(hideIdVal);
	        $("#" + hiddenName).val(hiddenName);
	    } else {
	        $("#" + params.key).contents().find("#" + inputId + "_text")
	                .val(inputNameVal);
	        $("#" + params.key).contents().find("#" + inputId).val(
	                hideIdVal);
	    } */
	}   
	
	   /**
	    * 判断该单位+年度+批次 已经设了已选类别限项的已经推荐的数量
	    */
	    function getOrgRecommendNum() {
	        var orgCode = $('#orgCode').val();
	        var batch = $('#batch').val();
	        var statYear = $('#statYear').val();
	        var grantCodes = $('#grantType_value').val();
	        var orgRecommendNum = 0;
	        $.ajax({
	            type: "POST",
	            async:false,
	            url: "${ctx}/prpapprove/getOrgRecommendNum",
	            data: {"orgCode":orgCode,"batch":batch,"statYear":statYear,"grantCodes":grantCodes},
	            beforeSend:function(XMLHttpRequest) {
//	                prcWin.openWin();//打开遮罩层
	            },
	            success: function(msg){
	                var obj = eval("(" + msg + ")");
	                orgRecommendNum = obj.orgRecommendNum;
	               /*  if(obj.isSuccess == 1) {
	                    isExists = obj.isExists == 1;
	                } else {
	                    scmError("校验单位限项是否存在该业务类别失败");
	                } */
	                
	            },// 表单成功提交后的回调函数
	            error : function() {
	                scmError("操作失败,请联系系统管理员");
	                $("input[type='button'][id!='back']").attr("disabled",false);
//	                 prcWin.closeWin();
	            }
	         });
	        return orgRecommendNum;
	    }
</script>